<?php

// @formatter:off
/**
 * @file cache.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-06-03 03:40:32 +0000 (Tue, 03 Jun 2014) $
 *
 * $Id: cache.php 112 2014-06-03 03:40:32Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/frontview/actions/content/cache.php $
 */
// @formatter:on

class ActionControllerContentCache extends WCActionController {
	//
	// Public methods.
	public function check() {
		$check = parent::check();

		if($check == WC_HTTPERROR_OK) {
			global $wcProfile;

			if(!$wcProfile->allowedTo(WC_PERM_CACHE_EDIT)) {
				global $errno;
				global $error;

				$check = WC_HTTPERROR_FORBIDDEN;
				$errno = $check;
				$error = "You're not allowed manage cache";
			}
			if($check == WC_HTTPERROR_OK && $_SERVER["REQUEST_METHOD"] == "POST") {
				if(isset($_POST["task"]) && $_POST["task"] == "flush" && !$wcProfile->allowedTo(WC_PERM_CACHE_FLUSH)) {
					global $errno;
					global $error;

					$check = WC_HTTPERROR_FORBIDDEN;
					$errno = $check;
					$error = "You're not allowed flush cache";
				}
			}
		}

		return $check;
	}
	//
	// Protected methods.
	protected function flush($id) {
		//
		// Loading global pointers.
		global $wcGroup;

		$db = WCConnection::Instance();
		$dbprefix = $db->prefix();
		//
		// @{
		$stmt = null;
		$stmtId = __CLASS__."::".__FUNCTION__;
		if($db->has($stmtId)) {
			$stmt = $db->get($stmtId);
		} else {
			$query = "update  {$dbprefix}mvc_cache\n";
			$query.= "set     mvcc_until = now()\n";
			$query.= "where   mvcc_group  = :group\n";
			$query.= " and    mvcc_action = :action\n";
			$stmt = $db->prepare($stmtId, $query);
		}
		// @}
		//
		// Building database parameters.
		$stmt->execute(array(
			":group" => $wcGroup->id(),
			":action" => $id
		));

		$this->_manager->entries = $stmt->rowCount();
	}
	protected function subRun() {
		$ok = true;
		//
		// Loading global pointers.
		global $wcGroup;

		$this->_manager->PATH = "Cache Status";

		$db = WCConnection::Instance();
		$dbprefix = $db->prefix();
		//
		// @{
		$stmt = null;
		$stmtId = __CLASS__."::".__FUNCTION__;
		if($db->has($stmtId)) {
			$stmt = $db->get($stmtId);
		} else {
			$query = "select  mvca_id,\n";
			$query.= "        mvca_type,\n";
			$query.= "        mvca_name,\n";
			$query.= "        mvca_skin,\n";
			$query.= "        count(1) as mvca_entries,\n";
			$query.= "        sum(length(mvcc_data)) as mvca_size\n";
			$query.= "from    {$dbprefix}mvc_cache\n";
			$query.= "                inner join {$dbprefix}mvc_actions\n";
			$query.= "                        on(mvcc_action = mvca_id)\n";
			$query.= "where   mvcc_until > now()\n";
			$query.= " and    mvcc_group in (0, :group)\n";
			$query.= "group by mvca_id, mvca_type, mvca_name, mvca_skin\n";
			$query.= "order by mvca_skin asc, mvca_name asc\n";
			$stmt = $db->prepare($stmtId, $query);
		}
		// @}
		//
		// Building database parameters.
		$params = array(
			":group" => $wcGroup->id()
		);
		//
		// Attempting to add the new member.
		$entries = array();
		if($stmt->execute($params) && $stmt->rowCount() > 0) {
			foreach($stmt->fetchAll() as $row) {
				$aux = array();
				foreach(array_keys($row)as $key) {
					$subKeys = explode("_", $key);
					$aux[$subKeys[1]] = $row[$key];
				}

				$aux["name"] = str_replace(array("ActionController", "LayoutController"), "", $aux["name"]);
				switch($aux["type"]) {
					case "A":
						$aux["type"] = $this->tr("label_action");
						break;
					case "L":
						$aux["type"] = $this->tr("label_layout");
						break;
				}
				$aux["size"] = bytesToHuman($aux["size"]);

				$entries[] = $aux;
			}
		}
		$this->_manager->entries = $entries;

		return $ok
		;
	}
	protected function subRunPOST() {
		$ok = true;

		switch(isset($_POST["task"]) ? $_POST["task"] : false) {
			case "flush":
				$this->_manager->status = true;
				$this->flush($_POST["id"]);
				break;
			default:
				$this->_manager->status = false;
				$this->_manager->error_message = $this->tr("message_unknown_task");
				break;
		}

		return $ok;
	}
}
